//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// (C) Copyright 2006 Marvell International Ltd.
// All Rights Reserved
//
/******************************************************************************
**
** INTEL CONFIDENTIAL
** Copyright 2003-2005 Intel Corporation All Rights Reserved.
**
** The source code contained or described herein and all documents
** related to the source code (Material) are owned by Intel Corporation
** or its suppliers or licensors.  Title to the Material remains with
** Intel Corporation or its suppliers and licensors. The Material contains
** trade secrets and proprietary and confidential information of Intel
** or its suppliers and licensors. The Material is protected by worldwide
** copyright and trade secret laws and treaty provisions. No part of the
** Material may be used, copied, reproduced, modified, published, uploaded,
** posted, transmitted, distributed, or disclosed in any way without Intel's
** prior express written permission.
**
** No license under any patent, copyright, trade secret or other intellectual
** property right is granted to or conferred upon you by disclosure or
** delivery of the Materials, either expressly, by implication, inducement,
** estoppel or otherwise. Any license under such intellectual property rights
** must be express and approved by Intel in writing.
**
**
******************************************************************************/

#include "SleepWrkArndWriteReg.h"
#include "string.h"


WORD 
SleepWAWriteRBit(WORD wTDIBit, WORD wTMSBit)
{
    volatile WORD *pMPU_Reg= (volatile WORD *)0xA8F50084; 


	if (wTDIBit == 1)
		wTDIBit = SIG_TDI;
	else if (wTDIBit == 0)
		wTDIBit = 0;
	else
		return (-1);
	if (wTMSBit == 1)
		wTMSBit = SIG_TMS;
	else if (wTMSBit == 0)
		wTMSBit = 0;
	else
		return (-1);
	
    *pMPU_Reg = SIG_ENABLE | wTDIBit | wTMSBit;
    *pMPU_Reg = SIG_ENABLE | wTDIBit | wTMSBit | SIG_TCK;
    *pMPU_Reg = 0;

	return (1);				   
}																	 
																	

int
SleepWAWriteRegister(WORD wPortal, char *psBitfield)
{
	WORD wTMSBit;
	int i,j;
	 
	SleepWAWriteRBit(0,0);
	
	SleepWAWriteRBit(0,1);

	SleepWAWriteRBit(0,1);

	SleepWAWriteRBit(0,0);

	SleepWAWriteRBit(0,0);

	for (i=0;i<11;i++)
	{
		if (i < 10)
			SleepWAWriteRBit((wPortal >> i) & 1, 0);
		else
			SleepWAWriteRBit((wPortal >> i) & 1, 1);
	}

	//	On the the last data shift, the loop sets TMS to 1 
	// causing the TAP controller to move to the "Exit1-IR" state

	// 	move the TAP controller to the "Update-IR" state
	SleepWAWriteRBit(0,1);

	if (psBitfield == NULL)
	{
   		// 	move the TAP controller to the "Run-Test/Idle" state
	   	SleepWAWriteRBit(0,0);
		return(0);
	}

	// 	move the TAP controller to the "Select-DR-Scan" state
	SleepWAWriteRBit(0,1);

	// 	move the TAP controller to the "Capture-DR" state
	SleepWAWriteRBit(0,0);

	// 	move the TAP controller to the "Shift-DR" state
	SleepWAWriteRBit(0,0);

	// clock in the data
	// remove possible bad character from end that would mess up wTMSBit check...
	i = strlen(psBitfield);
	while ( i && (psBitfield[i]=='_' || psBitfield[i]==' ') )
	{
		psBitfield[i] = 0;
		i--;
	}

	for (i=0;i<1000000;i++)
	{
		if (psBitfield[i] == '_' || psBitfield[i] == ' ')
			continue;	//skip human readability elements...
		if (psBitfield[i+1] == 0)
			wTMSBit = 1;
		else
			wTMSBit = 0;
		if (psBitfield[i] == '1')
			SleepWAWriteRBit(1,wTMSBit);
		else if (psBitfield[i] == '0')
			SleepWAWriteRBit(0,wTMSBit);
		else if (psBitfield[i] == 'F')
		{	for(j=0; j<4; j++)
				SleepWAWriteRBit(1,wTMSBit);
		}else if (psBitfield[i] == 0)
			break;
		else
			return (-1);
	}

	if (i == 1000000)
		return (-1);

	//	On the the last data shift, the loop sets TMS to 1 
	// causing the TAP controller to move to the "Exit1-DR" state

	// 	move the TAP controller to the "Update DR" state
	SleepWAWriteRBit(0,1);

	// 	move the TAP controller to the "Run-Test/Idle" state
	SleepWAWriteRBit(0,0);
	return (0);
}



